//
//  BGDanXuanCell.m
//  bigMall
//
//  Created by ios on 2016/12/9.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGDanXuanCell.h"
#import "BGReadStepTwoModel.h"

@interface BGDanXuanCell() {
    
     UILabel *titleLab;
     NSMutableArray *isUseArrM;
     NSMutableArray *isDefaultArrM;
     NSMutableArray *attributeArrM;
     NSMutableArray *changlePriceArrM;
    
    NSString *_fabuName;
    
}

@end

@implementation BGDanXuanCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier attr_values:(NSArray *)attr_values default_value:(NSArray *)default_value  fabuName:(NSString *)fabuName {
    
    isUseArrM = [NSMutableArray array];
    isDefaultArrM = [NSMutableArray array];
    attributeArrM = [NSMutableArray array];
    changlePriceArrM = [NSMutableArray array];
    _fabuName = fabuName;
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        //修饰线
        UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 1)];
        v1.backgroundColor = dockwhite_;
        [self.contentView addSubview:v1];
        
        titleLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 6, 100, 21)];
        titleLab.text = self.titleText;
        //设置标题的大小
//        CGSize biaotiSize = [titleLab.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleLab.font,NSFontAttributeName, nil]];
//        titleLab.frame = CGRectMake(21, 6, biaotiSize.width, biaotiSize.height);
        titleLab.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:titleLab];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 32, [UIScreen mainScreen].bounds.size.width, 1)];
        view.backgroundColor = dockwhite_;
        [self.contentView addSubview:view];
        
        //根据传进来的数据 for in 循环进行视图创建
//        for (BGReadStepTwoModelArrDetail *detalModel in attr_values) 
        for (int i = 0; i < attr_values.count; i++) {
            
            //取出ID赋值用Tag
            BGReadStepTwoModelArrDetail *detalModel = attr_values[i];
            
            //创建默认的按钮
            UIButton *isDefaultBtn = [[UIButton alloc] initWithFrame:CGRectMake(21, 10 + 32 * (i + 1), 50, 16)];
            [isDefaultBtn setTitle:@"默认" forState:UIControlStateNormal];
            isDefaultBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            isDefaultBtn.tag = [detalModel.ID intValue] + 1;
            isDefaultBtn.tintColor = [UIColor blueColor];
            isDefaultBtn.backgroundColor = dockwhite_;
            isDefaultBtn.clipsToBounds = YES;
            isDefaultBtn.layer.cornerRadius = 8;
            isDefaultBtn.userInteractionEnabled = NO;
            isDefaultBtn.titleEdgeInsets = UIEdgeInsetsMake(3, 3, 3, 3);
            [isDefaultBtn addTarget:self action:@selector(clickChanceIsDefault:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:isDefaultBtn];
            [isDefaultArrM addObject:isDefaultBtn];
            
            //创建属性的lable
            UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(isDefaultBtn.right + 10, 32 * (i + 1), 30, 16)];
            lable.tag = isDefaultBtn.tag;
            lable.font = [UIFont systemFontOfSize:14];
            lable.text = detalModel.value;
            CGSize size = [lable.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:lable.font,NSFontAttributeName, nil]];
            lable.frame = CGRectMake(isDefaultBtn.right + 10, 10 + (i + 1) * 32, size.width, 16);
            [self.contentView addSubview:lable];
            [attributeArrM addObject:lable];
            
            //创建修改的浮动价格的TextFiled
            UITextField *filed110 = [[UITextField alloc] initWithFrame:CGRectMake(lable.right + 10, lable.y, 88, 16)];
            filed110.borderStyle = UITextBorderStyleNone;
            filed110.placeholder = @"填写浮动值";
            filed110.font = [UIFont systemFontOfSize:14];
            filed110.userInteractionEnabled = NO;
            filed110.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
            filed110.tag = isDefaultBtn.tag;
            [filed110 addTarget:self action:@selector(filedChange:) forControlEvents:UIControlEventEditingDidEnd];
            [self.contentView addSubview:filed110];
            [changlePriceArrM addObject:filed110];
            
            //创建可使用按钮
            UIButton *isUseBtn = [[UIButton alloc] initWithFrame:CGRectMake(filed110.right + 5, 10 + 32 * (i + 1), 50, 16)];
            [isUseBtn setTitle:@"使用" forState:UIControlStateNormal];
            isUseBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            isUseBtn.tag = [detalModel.ID intValue] + 10;
            isUseBtn.tintColor = [UIColor blueColor];
            isUseBtn.backgroundColor = dockwhite_;
            isUseBtn.clipsToBounds = YES;
            isUseBtn.layer.cornerRadius = 8;
            [isUseBtn addTarget:self action:@selector(clickChanceIsUse:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:isUseBtn];
            [isUseArrM addObject:isUseBtn];
            
        }
        
        //开始赋默认
        for (BGReadStepTwoModelArrDetail *model in default_value) {
            
            //isDefault
            UIButton *btn_isDefault = isDefaultArrM[[model.ID integerValue]];
            if ([model.isDefault isEqualToString:@"1"]) {
                btn_isDefault.backgroundColor = [UIColor redColor];
                btn_isDefault.selected = YES;
            } else {
                btn_isDefault.backgroundColor = dockwhite_;
                btn_isDefault.selected = NO;
            }
            //changePrice
                UITextField *changePrcie_textFiled = changlePriceArrM[[model.ID integerValue]];
                changePrcie_textFiled.text = model.changePrice;
            //不是拍卖的才能交互
            if ([_fabuName isEqualToString:@"0"] || [_fabuName isEqualToString:@"2"]) {
                
                changePrcie_textFiled.userInteractionEnabled = YES;
                
            }
            //isUse
            UIButton *isUse_btn = isUseArrM[[model.ID integerValue]];
            if ([model.isUse isEqualToString:@"1"]) {
                isUse_btn.backgroundColor = [UIColor redColor];
                isUse_btn.selected = YES;
                //不是拍卖的才能交互
                if ([_fabuName isEqualToString:@"0"] || [_fabuName isEqualToString:@"2"]) {
                    
                    changePrcie_textFiled.userInteractionEnabled = YES;
                    
                }
                changePrcie_textFiled.text = model.changePrice;
                btn_isDefault.userInteractionEnabled = YES;
            } else {
                isUse_btn.backgroundColor = dockwhite_;
                isUse_btn.selected = NO;
                btn_isDefault.userInteractionEnabled = NO;
            }
            
        }
        
    }
    
    return self;
    
}

- (void)setTitleText:(NSString *)titleText {
    titleLab.text = titleText;
}
- (void)clickChanceIsUse:(UIButton *)btn {

    btn.selected = !btn.selected;
    
    UITextField *changePrcie_textFiled = changlePriceArrM[btn.tag - 10];
    UIButton *btn_isDefault = isDefaultArrM[btn.tag - 10];
    UILabel *lable = attributeArrM[btn.tag - 10];
    
    if (btn.selected) {
        
        [btn setBackgroundColor:[UIColor redColor]];
        
        //不是拍卖的才能交互
        if ([_fabuName isEqualToString:@"0"] || [_fabuName isEqualToString:@"2"]) {
            
            changePrcie_textFiled.userInteractionEnabled = YES;
            
        }
        
        changePrcie_textFiled.text = @"0.00";
        
        btn_isDefault.userInteractionEnabled = YES;
        
        /*
         这个位置添加一条代理方法
         **/
        if ([self.delegate respondsToSelector:@selector(attr_id:isDefault:isUse:changePrice:value:)]) {
            
            NSString *str;
            
            if (btn_isDefault.selected == YES) {
                str = @"true";
            }else {
                str = @"false";
            }
            
           [self.delegate attr_id:self.attr_id isDefault:str isUse:@"true" changePrice:changePrcie_textFiled.text value:lable.text];
        }
        
    } else {
        
        [btn setBackgroundColor:dockwhite_];
        
        changePrcie_textFiled.userInteractionEnabled = NO;
        
        changePrcie_textFiled.text = nil;
        
        btn_isDefault.userInteractionEnabled = NO;
        
        btn_isDefault.backgroundColor = dockwhite_;
        
        btn_isDefault.selected = NO;
        
        /*
         这个位置添加一条代理方法
         **/
        if ([self.delegate respondsToSelector:@selector(attr_id:isDefault:isUse:changePrice:value:)]) {
            
            NSString *str;
            
            if (btn_isDefault.selected == YES) {
                str = @"true";
            }else {
                str = @"false";
            }
            
            [self.delegate attr_id:self.attr_id isDefault:str isUse:@"false" changePrice:changePrcie_textFiled.text value:lable.text];
        }
    }
    
}
//单选的方法
- (void)clickChanceIsDefault:(UIButton *)btn {
    
    btn.selected = !btn.selected;
    
    UITextField *changePrcie_textFiled = changlePriceArrM[btn.tag - 1];
    UILabel *lable = attributeArrM[btn.tag - 1];
    
    if (btn.selected) {
        
        [btn setBackgroundColor:[UIColor redColor]];
        
        if ([self.delegate respondsToSelector:@selector(attr_id:isDefault:isUse:changePrice:value:)]) {
            
            [self.delegate attr_id:self.attr_id isDefault:@"true" isUse:@"true" changePrice:changePrcie_textFiled.text value:lable.text];
        
        for (int i = 0; i < isDefaultArrM.count; i++) {
            
            UIButton *isDefault_btn = isDefaultArrM[i];
            if (btn == isDefault_btn) {
                continue;
            }
            
            //把之前默认的数据传送上去
            if (isDefault_btn.selected == YES) {
                isDefault_btn.selected = NO;
                isDefault_btn.backgroundColor = dockwhite_;
                NSLog(@"%ld",(long)isDefault_btn.tag);
                
                
                /*
                 这个位置添加一条代理方法
                 **/
                if ([self.delegate respondsToSelector:@selector(attr_id:isDefault:isUse:changePrice:value:)]) {
                    
                    UITextField *text = changlePriceArrM[isDefault_btn.tag - 1];
                    UILabel *lab = attributeArrM[isDefault_btn.tag - 1];
                    
                    [self.delegate attr_id:self.attr_id isDefault:@"false" isUse:@"true" changePrice:text.text value:lab.text];
                }
            }
        }
       
    } else {
        
        [btn setBackgroundColor:dockwhite_];
        /*
         这个位置添加一条代理方法
         **/
        if ([self.delegate respondsToSelector:@selector(attr_id:isDefault:isUse:changePrice:value:)]) {
            
            [self.delegate attr_id:self.attr_id isDefault:@"false" isUse:@"true" changePrice:changePrcie_textFiled.text value:lable.text];
    }
    }
    }
}
- (void)filedChange:(UITextField *)textFiled {
    
    UIButton *btn_isDefault = isDefaultArrM[textFiled.tag - 1];
    UILabel *lable = attributeArrM[textFiled.tag - 1];
    /*
     这个位置添加一条代理方法
     **/
    if ([self.delegate respondsToSelector:@selector(attr_id:isDefault:isUse:changePrice:value:)]) {
        
        NSString *str;
        
        if (btn_isDefault.selected == YES) {
            str = @"true";
        }else {
            str = @"false";
        }

        [self.delegate attr_id:self.attr_id isDefault:str isUse:@"true" changePrice:textFiled.text value:lable.text];
    }
    
}
@end
